from flask import Flask,jsonify,render_template,request,escape
import requests,json
import pandas as pd
import cufflinks as cf
import plotly as py
import plotly.graph_objs as go
import io
import base64
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from pyecharts import options as opts
from pyecharts.charts import Bar
from jinja2 import Markup

app = Flask(__name__)

df = pd.read_excel("幸福感分析.xlsx", encoding = "utf8", sep="\t")
df_show = df.head(242)

@app.route("/",methods = ["GET"])
def base() ->"html":
	data_str = df_show.to_html()
	return render_template("base.html",
							the_res = data_str)


性别 = df.groupby ( by = ['1、您的性别：'] ) \
             .agg ({ "总分" : ["mean"], \
                     "PA总分":["mean"], \
                     "NA总分":["mean"], \
                     "PE总分":["mean"], \
                     "NE总分":["mean"],}) \
             .rename ( columns = {"mean":"均值"} )
年龄 = df.groupby ( by = ['2、您的年龄段：'] ) \
             .agg ({ "总分" : ["mean"], \
                     "PA总分":["mean"], \
                     "NA总分":["mean"], \
                     "PE总分":["mean"], \
                     "NE总分":["mean"],}) \
             .rename ( columns = {"mean":"均值"} )
居住地 = df.groupby ( by = ['3、您现在的居住地是：'] ) \
             .agg ({ "总分" : ["mean"], \
                     "PA总分":["mean"], \
                     "NA总分":["mean"], \
                     "PE总分":["mean"], \
                     "NE总分":["mean"],}) \
             .rename ( columns = {"mean":"均值"} )
婚姻 = df.groupby ( by = ['4、您的婚姻状况是：'] ) \
             .agg ({ "总分" : ["mean"], \
                     "PA总分":["mean"], \
                     "NA总分":["mean"], \
                     "PE总分":["mean"], \
                     "NE总分":["mean"],}) \
             .rename ( columns = {"mean":"均值"} )
居住方式 = df.groupby ( by = ['5、您的居住方式是：'] ) \
             .agg ({ "总分" : ["mean"], \
                     "PA总分":["mean"], \
                     "NA总分":["mean"], \
                     "PE总分":["mean"], \
                     "NE总分":["mean"],}) \
             .rename ( columns = {"mean":"均值"} )
文化 = df.groupby ( by = ['6、您的文化程度是：'] ) \
             .agg ({ "总分" : ["mean"], \
                     "PA总分":["mean"], \
                     "NA总分":["mean"], \
                     "PE总分":["mean"], \
                     "NE总分":["mean"],}) \
             .rename ( columns = {"mean":"均值"} )
收入 = df.groupby ( by = ['7、您现在的每个月收入（不包括子女给的）：'] ) \
             .agg ({ "总分" : ["mean"], \
                     "PA总分":["mean"], \
                     "NA总分":["mean"], \
                     "PE总分":["mean"], \
                     "NE总分":["mean"],}) \
             .rename ( columns = {"mean":"均值"} )
子女关系 = df.groupby ( by = ['9、您和孩子之间的关系：'] ) \
             .agg ({ "总分" : ["mean"], \
                     "PA总分":["mean"], \
                     "NA总分":["mean"], \
                     "PE总分":["mean"], \
                     "NE总分":["mean"],}) \
             .rename ( columns = {"mean":"均值"} )
身体 = df.groupby ( by = ['10、您目前的身体状况：'] ) \
             .agg ({ "总分" : ["mean"], \
                     "PA总分":["mean"], \
                     "NA总分":["mean"], \
                     "PE总分":["mean"], \
                     "NE总分":["mean"],}) \
             .rename ( columns = {"mean":"均值"} )
娱乐 = df.groupby ( by = ['11、您是否经常和朋友一起进行娱乐活动'] ) \
             .agg ({ "总分" : ["mean"], \
                     "PA总分":["mean"], \
                     "NA总分":["mean"], \
                     "PE总分":["mean"], \
                     "NE总分":["mean"],}) \
             .rename ( columns = {"mean":"均值"} )
兴趣 = df.groupby ( by = ['13、您是否有自己的兴趣爱好'] ) \
             .agg ({ "总分" : ["mean"], \
                     "PA总分":["mean"], \
                     "NA总分":["mean"], \
                     "PE总分":["mean"], \
                     "NE总分":["mean"],}) \
             .rename ( columns = {"mean":"均值"} )
性别_show = 性别.head()
年龄_show = 年龄.head()
居住地_show = 居住地.head()
婚姻_show = 婚姻.head()
居住方式_show = 居住方式.head()
文化_show = 文化.head()
收入_show = 收入.head()
子女关系_show = 子女关系.head()
身体_show = 身体.head()
娱乐_show = 娱乐.head()
兴趣_show = 兴趣.head()

tp1 = 性别.reset_index().plot.bar(x = '1、您的性别：',y = "总分")

@app.route("/a",methods = ["GET"])
def a() ->"html":
	bg1 = 性别_show.to_html()
	bg2 = 年龄_show.to_html()
	bg3 = 居住地_show.to_html()
	bg4 = 婚姻_show.to_html()
	bg5 = 居住方式_show.to_html()
	bg6 = 文化_show.to_html()
	bg7 = 收入_show.to_html()
	bg8 = 子女关系_show.to_html()
	bg9 = 身体_show.to_html()
	bg10 = 娱乐_show.to_html()
	bg11 = 兴趣_show.to_html()
	return render_template("a.html",
							bg1 = bg1,
							bg2 = bg2,
							bg3 = bg3,
							bg4 = bg4,
							bg5 = bg5,
							bg6 = bg6,
							bg7 = bg7,
							bg8 = bg8,
							bg9 = bg9,
							bg10 = bg10,
							bg11 = bg11,)

def bar_base1() -> Bar:
    tp1 = (
        Bar()
            .add_xaxis(["总分", "PA总分", "NA总分", "PE总分", "NE总分"])
            .add_yaxis("男", [24.5, 5.0, 4.7, 7.1, 6.9])
            .add_yaxis("女", [25.4, 5.4, 4.6, 7.1, 6.5])
            .set_global_opts(title_opts=opts.TitleOpts(title="性别与幸福感关系", subtitle="均值"))
    )
    return tp1

@app.route("/tp1")
def tp1():
	c = bar_base1()
	return Markup(c.render_embed())

def bar_base2() -> Bar:
    tp2 = (
        Bar()
            .add_xaxis(["总分", "PA总分", "NA总分", "PE总分", "NE总分"])
            .add_yaxis("60-69岁", [24.3, 5.1, 5.0, 6.9, 6.7])
            .add_yaxis("70-79岁", [25.3, 5.0, 4.3, 7.3, 6.6])
            .add_yaxis("80岁以上", [25.1, 5.5, 4.7, 7.2, 6.9])
            .set_global_opts(title_opts=opts.TitleOpts(title="年龄与幸福感关系", subtitle="均值"))
    )
    return tp2

@app.route("/tp2")
def tp2():
	c = bar_base2()
	return Markup(c.render_embed())

def bar_base3() -> Bar:
    tp3 = (
        Bar()
            .add_xaxis(["总分", "PA总分", "NA总分", "PE总分", "NE总分"])
            .add_yaxis("东北地区（黑龙江省、吉林省、辽宁省）", [23.6, 4.8, 5.0, 7.3, 7.5])
            .add_yaxis("华东地区（上海市、江苏省、浙江省、安徽省、福建省、江西省、山东省、台湾省）", [26.0, 5.5, 4.0, 6.9, 6.4])
            .add_yaxis("华中地区（河南省、湖北省、湖南省）", [26.6, 5.2, 4.3, 6.9, 5.2])
            .add_yaxis("华北地区（北京市、天津市、山西省、河北省、内蒙古自治区）", [24.5, 4.9, 5.0, 7.3, 6.7])
            .add_yaxis("华南地区（广东省、广西壮族自治区、海南省、香港特别行政区、澳门特别行政区）", [25.4, 5.4, 5.0, 7.6, 6.6])
            .add_yaxis("西北地区（陕西省、甘肃省、青海省、宁夏回族自治区、新疆维吾尔自治区）", [24.1, 5.9, 5.0, 6.8, 7.4])
            .add_yaxis("西南地区（四川省、贵州省、云南省、重庆市、西藏自治区）", [23.9, 5.1, 5.0, 6.9, 7.0])
            .set_global_opts(title_opts=opts.TitleOpts(title="居住地与幸福感关系", subtitle="均值"))
    )
    return tp3

@app.route("/tp3")
def tp3():
	c = bar_base3()
	return Markup(c.render_embed())

def bar_base4() -> Bar:
    tp4 = (
        Bar()
            .add_xaxis(["总分", "PA总分", "NA总分", "PE总分", "NE总分"])
            .add_yaxis("丧偶", [28.0, 6.0, 4.4, 8.1, 5.8])
            .add_yaxis("已婚", [24.6, 5.1, 4.6, 6.9, 6.8])
            .add_yaxis("未婚", [26.6, 6.8, 6.0, 10.6, 8.8])
            .add_yaxis("离婚", [23.4, 4.3, 4.6, 6.6, 6.8])
            .set_global_opts(title_opts=opts.TitleOpts(title="婚姻状况与幸福感关系", subtitle="均值"))
    )
    return tp4

@app.route("/tp4")
def tp4():
	c = bar_base4()
	return Markup(c.render_embed())

def bar_base5() -> Bar:
    tp5 = (
        Bar()
            .add_xaxis(["总分", "PA总分", "NA总分", "PE总分", "NE总分"])
            .add_yaxis("住在养老院", [24.1, 6.0, 4.9, 6.5, 6.3])
            .add_yaxis("和子女居住在一起", [24.9, 5.1, 7.3, 6.9, 7.1])
            .add_yaxis("和配偶居住在一起", [24.7, 5.2, 4.6, 7.0, 6.8])
            .add_yaxis("独自一个人居住", [25.8, 5.5, 4.6, 7.4, 6.4])
            .set_global_opts(title_opts=opts.TitleOpts(title="居住方式与幸福感关系", subtitle="均值"))
    )
    return tp5

@app.route("/tp5")
def tp5():
	c = bar_base5()
	return Markup(c.render_embed())

def bar_base6() -> Bar:
    tp6 = (
        Bar()
            .add_xaxis(["总分", "PA总分", "NA总分", "PE总分", "NE总分"])
            .add_yaxis("文盲", [24.7, 5.3, 4.2, 5.9, 6.2])
            .add_yaxis("小学", [25.6, 5.4, 4.7, 7.8, 6.7])
            .add_yaxis("中专或技校", [24.0, 5.0, 5.1, 6.9, 6.8])
            .add_yaxis("初中", [23.4, 5.0, 5.0, 6.8, 7.4])
            .add_yaxis("高中", [25.4, 5.0, 4.4, 7.4, 6.4])
            .add_yaxis("大专或者本科", [29.2, 6.2, 4.2, 9.2, 5.9])
            .add_yaxis("硕士研究生及以上学历", [23.8, 4.8, 4.6, 6.9, 7.3])
            .set_global_opts(title_opts=opts.TitleOpts(title="文化程度与幸福感关系", subtitle="均值"))
    )
    return tp6

@app.route("/tp6")
def tp6():
	c = bar_base6()
	return Markup(c.render_embed())

def bar_base7() -> Bar:
    tp7 = (
        Bar()
            .add_xaxis(["总分", "PA总分", "NA总分", "PE总分", "NE总分"])
            .add_yaxis("1000元以下", [24.8, 5.0, 4.5, 6.9, 6.6])
            .add_yaxis("1000-2500元", [26.4, 5.2, 3.9, 7.5, 6.4])
            .add_yaxis("2500-4000元", [23.2, 4.8, 5.3, 6.5, 6.8])
            .add_yaxis("4000-5500元", [24.3, 5.0, 4.9, 6.8, 6.6])
            .add_yaxis("5500-7000元", [25.0, 5.3, 4.6, 6.7, 6.3])
            .add_yaxis("7000-8500元", [23.4, 5.1, 5.5, 7.1, 7.3])
            .add_yaxis("8500-10000元", [24.6, 5.5, 5.4, 7.1, 6.6])
            .add_yaxis("10000元以上", [26.0, 5.7, 3.9, 7.6, 7.4])
            .set_global_opts(title_opts=opts.TitleOpts(title="收入与幸福感关系", subtitle="均值"))
    )
    return tp7

@app.route("/tp7")
def tp7():
	c = bar_base7()
	return Markup(c.render_embed())

def bar_base8() -> Bar:
    tp8 = (
        Bar()
            .add_xaxis(["总分", "PA总分", "NA总分", "PE总分", "NE总分"])
            .add_yaxis("关系一般", [23.7, 4.9, 5.0, 6.9, 7.1])
            .add_yaxis("关系较差", [23.3, 4.8, 4.9, 7.2, 7.8])
            .add_yaxis("很亲近", [27.0, 5.6, 4.1, 7.3, 5.8])
            .add_yaxis("比较亲近", [24.8, 5.2, 4.7, 7.0, 6.8])
            .set_global_opts(title_opts=opts.TitleOpts(title="子女关系与幸福感关系", subtitle="均值"))
    )
    return tp8

@app.route("/tp8")
def tp8():
	c = bar_base8()
	return Markup(c.render_embed())

def bar_base9() -> Bar:
    tp9 = (
        Bar()
            .add_xaxis(["总分", "PA总分", "NA总分", "PE总分", "NE总分"])
            .add_yaxis("一般", [24.5, 4.9, 4.8, 7.0, 6.5])
            .add_yaxis("比较健康", [24.4, 5.1, 4.7, 7.0, 7.0])
            .add_yaxis("身体不好", [23.0, 5.0, 5.0, 6.6, 7.6])
            .add_yaxis("非常健康", [27.1, 5.7, 4.1, 7.6, 6.1])
            .set_global_opts(title_opts=opts.TitleOpts(title="身体状况与幸福感关系", subtitle="均值"))
    )
    return tp9

@app.route("/tp9")
def tp9():
	c = bar_base9()
	return Markup(c.render_embed())

def bar_base10() -> Bar:
    tp10 = (
        Bar()
            .add_xaxis(["总分", "PA总分", "NA总分", "PE总分", "NE总分"])
            .add_yaxis("从不", [24.4, 5.0, 4.8, 7.1, 6.9])
            .add_yaxis("偶尔", [25.1, 5.1, 4.4, 6.9, 6.5])
            .add_yaxis("经常", [25.2, 5.5, 4.7, 7.3, 6.9])
            .set_global_opts(title_opts=opts.TitleOpts(title="娱乐活动与幸福感关系", subtitle="均值"))
    )
    return tp10

@app.route("/tp10")
def tp10():
	c = bar_base10()
	return Markup(c.render_embed())

def bar_base11() -> Bar:
    tp11 = (
        Bar()
            .add_xaxis(["总分", "PA总分", "NA总分", "PE总分", "NE总分"])
            .add_yaxis("否", [23.4, 5.0, 5.0, 6.9, 7.4])
            .add_yaxis("是", [26.7, 5.5, 4.2, 7.3, 5.8])
            .set_global_opts(title_opts=opts.TitleOpts(title="兴趣爱好与幸福感关系", subtitle="均值"))
    )
    return tp11

@app.route("/tp11")
def tp11():
	c = bar_base11()
	return Markup(c.render_embed())

if __name__ == '__main__':
	app.run(debug=True)